﻿

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>用户管理</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<script type ="text/javascript">


	Ext.onReady(function()
	{
		//Ext.namespace("com.group1");
		var grid ;
		var win_update ;
		var win_add;
		var store;

		
		var ht = Ext.get("main_tree").getHeight() -25;
		var addImg = getAddImg();
		var updateImg =getUpdateImg();
		var delImg =getDelImg();

		var toolBar1 = new Ext.Toolbar(
		{
			buttons:
			[
				{
					text:" 增加 ",
					icon:addImg,cls:"x-btn-text-icon" ,
					cls: "x-btn-text-icon",
					handler: function()
					{
						var proxy = new Ext.data.HttpProxy({url:"Json/AppAction!listAll"});
						var reader =new Ext.data.JsonReader(
						{},
						[
							{name:"app_id",type:"string",mapping:"app_id"},
							{name:"app_name",type:"string",mapping:"app_name"}
						]);
						
						var g_store =new Ext.data.Store(
						{
							proxy:proxy,
							reader:reader
						});
						

						g_store.load({
						 callback : function(records) {
							 var firstValue = g_store.reader.jsonData[0].app_id;
								 f_app.setValue(firstValue);
							 }
						 });
						   var f_app = new Ext.form.ComboBox({
							fieldLabel : "所在分组",
							id:'f_app',
								store: g_store,
							displayField:"app_name",
							valueField:"app_id",
							hiddenName : 'f_app_id',
							mode:"remote",
							
							emptyText : "请选择现场程序",
							allowBlank : false,
							blankText : "现场程序不能为空，请选择！",
							triggerAction : "all" // 每次选中一项，之后再选也会将所有列表显示出来。
							
						}); 
						
						var txt_com_name = new Ext.form.TextField({
							fieldLabel: "串口名称",
							name: "f_com_name",
							allowBlank: false
							});
						var txt_com_id = new Ext.form.TextField({
							fieldLabel: "串口号",
							name: "f_com_id",
							allowBlank: false
							});

						var txt_com_baud = new Ext.form.TextField({
							fieldLabel: "波特率",
							name: "f_com_baud",
							allowBlank: false
							});

							
						var cmb_com_parity = new Ext.form.ComboBox({
							fieldLabel : "校验位",			
							id : "cmb_com_parity",
							mode:"local",
							width : 145,
							editable : false,
							value:0,
							store : new Ext.data.SimpleStore({
							fields : ['id', 'data'],
							data :
							[
								["0","N无"],
								["1","O奇"],
								["2","E偶"],
								["3","M标识"],
								["4","S空格"]
							]
							}),
							displayField: 'data',
							valueField: 'id',
							hiddenName : 'f_com_parity',//这里千万不要与id同名，不然服务器接收的值异常。
							emptyText : "请选择校验位",
							allowBlank : false,
							blankText : "校验位不能为空，请选择！",
							triggerAction : "all"
						}); 	
	
						var cmb_com_data = new Ext.form.ComboBox({
							fieldLabel : "数据位",			
							id : "cmb_com_data",
							mode:"local",
							width : 145,
							editable : false,
							value:4,
							store : new Ext.data.SimpleStore({
							fields : ['id', 'data'],
							data :
							[
								["4","4"],
								["5","5"],
								["6","6"],
								["7","7"],
								["8","8"]
							]
							}),
							displayField: 'data',
							valueField: 'id',
							hiddenName : 'f_com_data',//这里千万不要与id同名，不然服务器接收的值异常。
							emptyText : "请选择数据位",
							allowBlank : false,
							blankText : "数据位不能为空，请选择！",
							triggerAction : "all"
						}); 

						var cmb_com_stop = new Ext.form.ComboBox({
							fieldLabel : "停止位",			
							id : "cmb_com_stop",
							mode:"local",
							width : 145,
							editable : false,
							value:0,
							store : new Ext.data.SimpleStore({
							fields : ['id', 'data'],
							data :
							[
								["0","1"],
								["1","1.5"],
								["2","2"]
							]
							}),
							displayField: 'data',
							valueField: 'id',
							hiddenName : 'f_com_stop',//这里千万不要与id同名，不然服务器接收的值异常。
							emptyText : "请选择校验位",
							allowBlank : false,
							blankText : "校验位不能为空，请选择！",
							triggerAction : "all"
						}); 
								
						var form_add = new Ext.form.FormPanel({
							frame: true,
							items: [txt_com_name,txt_com_id,txt_com_baud,cmb_com_parity,cmb_com_data,cmb_com_stop,f_app], 
							url: "Json/ComportAction!add",
							baseParams: {method: "edit"},
							method: "post",
							buttons: 
								[
								{
									text: "增加",
									handler: function()
									{
										var json = 
										{
											success: function(f, action)
											{
												Ext.Msg.alert("成功", action.result.msg);
												store.load({params: {start: 0, limit: 10}});
												//store.reload();
												win_add.close();
											},
											failure: function(f,action)	
											{
													Ext.Msg.alert("失败", action.result.msg);
											}
										};
										form_add.getForm().submit(json);
									}
								},
								{
									text: "关闭",
									handler: function()
									{
										win_add.close();
									}
								}
								]
								}); // form结束
								//////////////////////////////////////////////
								
								
								win_add = new Ext.Window({
									title: "增加",
									width: 500,
									modal: true,
									autoHeight: true,
									items: [form_add]
									}); 

								win_add.show();
								//form_add.getForm().setValues({"f_companyname",});	
					}
				},
				{xtype:"tbseparator"},
				{
					text:" 编辑 ",
					icon:updateImg,cls:"x-btn-text-icon", 
					cls: "x-btn-text-icon",
					handler: function()
					{
						//判断是否有选择行
						var selModel = grid.getSelectionModel();
						var record;//选择的行的数据
						if(!selModel.hasSelection())
						{
							Ext.Msg.alert("错误", "请选择要修改的行!");
						}
						else if(selModel.getSelections().length > 1)
						{
							Ext.Msg.alert("错误", "一次只能修改一行,不行同时选择多行!");
						}
						else
						{
							record = selModel.getSelected();
							//Ext.Msg.alert("成功", "修改的ID为"+record.get("SysID"));
							
							//////////////////////////////////////////////
							//定义隐藏表单域保存修改管理员的 ID，f表示request取的变量
							//var txt_fid = new Ext.form.Hidden({name: "f_id"});
							//txt_fcompanyname.setDisabled(true);
							var proxy = new Ext.data.HttpProxy({url:"Json/AppAction!listAll"});
							var reader =new Ext.data.JsonReader(
							{},
							[
								{name:"app_id",type:"string",mapping:"app_id"},
								{name:"app_name",type:"string",mapping:"app_name"}
							]);
							
							var g_store =new Ext.data.Store(
							{
								proxy:proxy,
								reader:reader
							});
							

							g_store.load({
							 callback : function(records) {
								 var firstValue = g_store.reader.jsonData[0].app_id;
									 f_app.setValue(firstValue);
								 }
							 });
							   var f_app = new Ext.form.ComboBox({
								fieldLabel : "所在分组",
								id:'f_app',
									store: g_store,
								displayField:"app_name",
								valueField:"app_id",
								hiddenName : 'f_app_id',
								mode:"remote",
								
								emptyText : "请选择现场程序",
								allowBlank : false,
								blankText : "现场程序不能为空，请选择！",
								triggerAction : "all" // 每次选中一项，之后再选也会将所有列表显示出来。
								
							}); 
							
						var txt_com_name = new Ext.form.TextField({
							fieldLabel: "串口名称",
							name: "f_com_name",
							allowBlank: false
							});
							var txt_com_id = new Ext.form.TextField({
								fieldLabel: "串口号",
								name: "f_com_id",
								allowBlank: false
								});
							var txt_com_baud = new Ext.form.TextField({
								fieldLabel: "波特率",
								name: "f_com_baud",
								allowBlank: false
								});

								
							var cmb_com_parity = new Ext.form.ComboBox({
								fieldLabel : "校验位",			
								id : "cmb_com_parity",
								mode:"local",
								width : 145,
								editable : false,
								value:0,
								store : new Ext.data.SimpleStore({
								fields : ['id', 'data'],
								data :
								[
									["0","N无"],
									["1","O奇"],
									["2","E偶"],
									["3","M标识"],
									["4","S空格"]
								]
								}),
								displayField: 'data',
								valueField: 'id',
								hiddenName : 'f_com_parity',//这里千万不要与id同名，不然服务器接收的值异常。
								emptyText : "请选择校验位",
								allowBlank : false,
								blankText : "校验位不能为空，请选择！",
								triggerAction : "all"
							}); 	
		
							var cmb_com_data = new Ext.form.ComboBox({
								fieldLabel : "数据位",			
								id : "cmb_com_data",
								mode:"local",
								width : 145,
								editable : false,
								value:4,
								store : new Ext.data.SimpleStore({
								fields : ['id', 'data'],
								data :
								[
									["4","4"],
									["5","5"],
									["6","6"],
									["7","7"],
									["8","8"]
								]
								}),
								displayField: 'data',
								valueField: 'id',
								hiddenName : 'f_com_data',//这里千万不要与id同名，不然服务器接收的值异常。
								emptyText : "请选择数据位",
								allowBlank : false,
								blankText : "数据位不能为空，请选择！",
								triggerAction : "all"
							}); 

							var cmb_com_stop = new Ext.form.ComboBox({
								fieldLabel : "停止位",			
								id : "cmb_com_stop",
								mode:"local",
								width : 145,
								editable : false,
								value:0,
								store : new Ext.data.SimpleStore({
								fields : ['id', 'data'],
								data :
								[
									["0","1"],
									["1","1.5"],
									["2","2"]
								]
								}),
								displayField: 'data',
								valueField: 'id',
								hiddenName : 'f_com_stop',//这里千万不要与id同名，不然服务器接收的值异常。
								emptyText : "请选择校验位",
								allowBlank : false,
								blankText : "校验位不能为空，请选择！",
								triggerAction : "all"
							}); 

						

								
							var form = new Ext.form.FormPanel({
								frame: true,
								items: [txt_com_name,txt_com_id,txt_com_baud,cmb_com_parity,cmb_com_data,cmb_com_stop,f_app], 
								url: "Json/ComportAction!update?p_id="+record.get("id"),
								baseParams: {method: "edit"},
								method: "post",
								buttons: 
									[
									{
										text: "修改",
										handler: function()
										{
											var json = 
											{
												success: function(f, action)
												{
													Ext.Msg.alert("成功", action.result.msg);
													store.reload();
													win_update.close();
												},
												failure: function(f,action)	
												{
													Ext.Msg.alert("失败", action.result.msg);
													//store.reload();
													//Ext.Msg.alert("失败", "对不起，操作失败，请检查数据是否完整！");
												}
											};
											form.getForm().submit(json);
										}
									},
									{
										text: "关闭",
										handler: function()
										{
											win_update.close();
										}
									}
									]
								}); // form结束
								//////////////////////////////////////////////
								form.getForm().load({  
        							    waitTitle : "请稍候",  
         							    waitMsg : "正在加载表单数据，请稍候......",  
        							    url : "Json/ComportAction!query?p_id="+record.get("id"), 
     								    success : function(form, action) { 
									  },  
       								    failure : function(form, action) {  
             							  	 Ext.MessageBox.alert('加载失败', action.result.errors);  
        						    		}  
       								}); 
								
								win_update = new Ext.Window({
									title: "编辑",
									width: 500,
									modal: true,
									autoHeight: true,
									items: [form]
									}); 

								win_update.show();
								
						}
					}
					

				},
				{xtype:"tbseparator"},
				{
					text:" 删除 ",
					icon:delImg,cls:"x-btn-text-icon" ,
					cls: "x-btn-text-icon",
					handler: function()
					{
												//判断是否有选择行
						var selModel = grid.getSelectionModel();
						var record;//选择的行的数据
						if(!selModel.hasSelection())
						{
							Ext.Msg.alert("失败", "请选择要删除的行!");
						}
						else 
						{
							
							var rows = grid.getSelectionModel().getSelections();// 返回值为 Record 数组
							if(rows.length==0)
							{
								Ext.MessageBox.alert('警告', '最少选择一条信息，进行删除!');
							}
							else
							{
									Ext.MessageBox.confirm('提示框', '您确定要进行该操作？',function(btn)
									{
										if(btn=='yes')
										{
											if(rows)
											{
												for (var i = 0; i < rows.length; i++) 
												{
													//alert(rows[i].get("SysManager"));
													store.remove(rows[i]);
													//store.removeAll(); //删除所有数据 
												}
											}
											
										}
									});
							
							}
									
							
						}
					}
					
					
					
				}
				
			]
		});
		
		
		var sm = new Ext.grid.CheckboxSelectionModel();
		
		var cm = new Ext.grid.ColumnModel(
		[
			
			new Ext.grid.RowNumberer(),
			sm,
			{header: "编号", width: 120, dataIndex: "id"},
			{header: "串口名称", width: 120, dataIndex: "com_name"},
			{header: "串口号", width: 120, dataIndex: "com_id"},
			{header: "波特率", width: 120, dataIndex: "com_baud"},
			{header: "校验位", width: 120, dataIndex: "com_parity"},
			{header: "数据位 ", width: 120, dataIndex: "com_data"},
			{header: "停止位", width: 120, dataIndex: "com_stop"},
			{id: "app_name",header: "现场程序名称", width: 120, dataIndex: "app_name" }
		]);

		
		
		var proxy = new Ext.data.HttpProxy({url: "Json/ComportAction!list"});
		//Record	定义记录结果
		var Manager = Ext.data.Record.create([
			{name: "id", type: "string", mapping: "id"},
			{name: "com_name", type: "string", mapping: "com_name"},
			{name: "com_id", type: "string", mapping: "com_id"},
			{name: "com_baud", type: "string", mapping: "com_baud"},
			{name: "com_parity", type: "string", mapping: "com_parity"},
			{name: "com_data", type: "string", mapping: "com_data"},
			{name: "com_stop", type: "string", mapping: "com_stop"},
			{name: "app_name", type: "string", mapping: "app_name"}
		]);
		
		
		var reader = new Ext.data.JsonReader(
		{totalProperty: "totalProperty", root: "root"},   
		Manager 
		);

		//Store
		store = new Ext.data.Store
		({
		proxy: proxy,
		reader: reader
		});
		//store.load();
		store.load({params: {start: 0, limit: 10}});

		grid = new Ext.grid.GridPanel({
			region:'center',
			//title: "中国公民",
			width:600,
   			height:300,
			cm: cm,
			sm: sm,
			store: store,
			//renderTo: "group1_managelist",
			autoExpandColumn: "app_name", //自动伸展，占满剩余区域
			tbar:toolBar1,
			bbar: new Ext.PagingToolbar({
					store: store, //这个不用介绍了吧？
					pageSize: 10,//页大小
					displayInfo: true,//是否显示 displayMsg
					displayMsg: "本页显示第{0}条到第{1}条的记录,一共{2}条",
					emptyMsg: "没有记录"//如果没有记录，则显示该文本
				}),
			listeners:
			{

			}
			
		});	
		
		grid.getStore().on("remove", function(s, record, index)
		{		
			var jsonArray = [record.data];
			Ext.Ajax.request({
			method: "post",
			url: "Json/ComportAction!delete",
			params: {f_data: Ext.util.JSON.encode(jsonArray)},
			success:function(response,opts) 
			{
			
				var jsonObj=Ext.util.JSON.decode(response.responseText );
				if(jsonObj.success==true)
				{
					store.reload();
					Ext.Msg.alert("成功", "删除成功!");
					
				}
				else
				{
					store.reload();
					Ext.Msg.alert("删除失败",jsonObj.msg);
					
				}
				
			}
			
			});
		});  

		
		
		var panel1 = new Ext.Panel({
		 //layout:'border',
		 layout:'fit',//自适应
  		items: [grid],
		//title:"最简单的面板11111111",
		//width:600,
		height:ht,
		titleCollapse:true,
		collapsible:false,
		renderTo:"comport_list"
		});
		
	});	
	
			
	</script>
  </head>
  
  <body>
     <div id="comport_list"></div>
  </body>
</html>
